package bev.frames.login;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
import javax.swing.filechooser.FileNameExtensionFilter;

import java.awt.GridBagLayout;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

import bev.ConnectionManager;

public class LoginFrame extends JDialog {

    private static final long serialVersionUID = -6838169081604078595L;
    private final JPanel contentPanel = new JPanel();
    private JTextField name;
    private JTextField URL;
    private JTextField username;
    private JPasswordField password;
    private static String title = "Connect to New Project";

    /**
     * Create the dialog.
     */
    public LoginFrame( JFrame parent, boolean modal ) {
        super( parent, title, modal );

        // Configure frame.
        this.setResizable( false );
        setBounds( 100, 100, 450, 300 );

        // Use border layout.
        getContentPane().setLayout( new BorderLayout() );
        contentPanel.setBorder( new EmptyBorder( 5, 5, 5, 5 ) );
        getContentPane().add( contentPanel, BorderLayout.CENTER );

        GridBagLayout gbl_contentPanel = new GridBagLayout();
        gbl_contentPanel.columnWidths = new int[] { 180, 180 };
        // gbl_contentPanel.rowHeights = new int[] { 35, 35, 35, 35, 35, 35 };
        contentPanel.setLayout( gbl_contentPanel );
        {
            JLabel name = new JLabel( "Name" );
            GridBagConstraints gbc_lbl = new GridBagConstraints();
            gbc_lbl.insets = new Insets( 3, 10, 3, 10 );
            gbc_lbl.anchor = GridBagConstraints.WEST;
            gbc_lbl.gridx = 0;
            gbc_lbl.gridy = 0;
            contentPanel.add( name, gbc_lbl );
        }
        {
            JLabel lbl = new JLabel( "URL" );
            GridBagConstraints gbc_lbl = new GridBagConstraints();
            gbc_lbl.insets = new Insets( 5, 10, 3, 10 );
            gbc_lbl.anchor = GridBagConstraints.WEST;
            gbc_lbl.gridx = 0;
            gbc_lbl.gridy = 2;
            contentPanel.add( lbl, gbc_lbl );
        }
        {
            JLabel name = new JLabel( "Username" );
            GridBagConstraints gbc_lbl = new GridBagConstraints();
            gbc_lbl.insets = new Insets( 5, 10, 3, 5 );
            gbc_lbl.anchor = GridBagConstraints.WEST;
            gbc_lbl.gridx = 0;
            gbc_lbl.gridy = 4;
            contentPanel.add( name, gbc_lbl );
        }
        {
            JLabel name = new JLabel( "Password" );
            GridBagConstraints gbc_lbl = new GridBagConstraints();
            gbc_lbl.insets = new Insets( 5, 5, 3, 10 );
            gbc_lbl.anchor = GridBagConstraints.WEST;
            gbc_lbl.gridx = 1;
            gbc_lbl.gridy = 4;
            contentPanel.add( name, gbc_lbl );
        }
        {
            name = new JTextField();
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.insets = new Insets( 3, 10, 3, 10 );
            gbc.fill = GridBagConstraints.HORIZONTAL;
            gbc.gridx = 0;
            gbc.gridy = 1;
            gbc.gridwidth = 2;
            contentPanel.add( name, gbc );
        }
        {
            URL = new JTextField();
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.insets = new Insets( 3, 10, 3, 10 );
            gbc.fill = GridBagConstraints.HORIZONTAL;
            gbc.gridx = 0;
            gbc.gridy = 3;
            gbc.gridwidth = 2;
            contentPanel.add( URL, gbc );
        }
        {
            username = new JTextField();
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.insets = new Insets( 3, 10, 5, 5 );
            gbc.fill = GridBagConstraints.HORIZONTAL;
            gbc.gridx = 0;
            gbc.gridy = 5;
            contentPanel.add( username, gbc );
        }
        {
            password = new JPasswordField();
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.insets = new Insets( 3, 5, 5, 10 );
            gbc.fill = GridBagConstraints.HORIZONTAL;
            gbc.gridx = 1;
            gbc.gridy = 5;
            contentPanel.add( password, gbc );
        }

        {
            JPanel buttonPane = new JPanel();
            buttonPane.setLayout( new FlowLayout( FlowLayout.RIGHT ) );
            buttonPane.setBorder( new EmptyBorder( 0, 10, 5, 10 ) );
            getContentPane().add( buttonPane, BorderLayout.SOUTH );
            {
                JButton okButton = new JButton( "OK" );
                okButton.addActionListener( new OKLogin( this ) );
                okButton.setActionCommand( "OK" );
                buttonPane.add( okButton );
                getRootPane().setDefaultButton( okButton );
            }
            {
                JButton cancelButton = new JButton( "Cancel" );
                cancelButton.addActionListener( new CancelLogin( this ) );
                cancelButton.setActionCommand( "Cancel" );
                buttonPane.add( cancelButton );
            }
        }

        this.pack();
    }

    public String getURL() {
        return this.URL.getText();
    }

    public String getUsername() {
        return this.username.getText();
    }

    public String getPassword() {
        return new String( this.password.getPassword() );
    }

    public String getName() {
        return this.name.getText();
    }

    class CancelLogin implements ActionListener {

        private JDialog parent;

        public CancelLogin( JDialog parentDialog ) {
            this.parent = parentDialog;
        }

        public void actionPerformed( ActionEvent e ) {
            parent.dispose();
        }
    }

    class OKLogin implements ActionListener {

        private LoginFrame parent;

        public OKLogin( LoginFrame parentDialog ) {
            this.parent = parentDialog;
        }

        public void actionPerformed( ActionEvent e ) {
            String t_pass = parent.getPassword();
            if ( ConnectionManager.testDatabaseConnection( parent.getURL(),
                    parent.getUsername(), t_pass ) ) {
                ConnectionManager.setDatabaseInfo( parent.getURL(),
                        parent.getUsername(), t_pass );

                parent.setVisible( false );

                int results = JOptionPane.showConfirmDialog( parent,
                        "Would you like to save this connection?",
                        "Save Connection?", JOptionPane.OK_CANCEL_OPTION );
                if ( results == 0 ) {
                    JFileChooser fileChooser = new JFileChooser();
                    FileNameExtensionFilter vwFilter = new FileNameExtensionFilter(
                            "Virtual World (*.vw)", "vw" );
                    fileChooser.setFileFilter( vwFilter );

                    if ( fileChooser.showSaveDialog( parent ) == JFileChooser.APPROVE_OPTION ) {
                        if ( !ConnectionManager.writeConnectionFile( String
                                .format( "%s.%s", fileChooser.getSelectedFile()
                                        .getAbsolutePath(), "vw" ) ) ) {
                            JOptionPane.showMessageDialog( parent,
                                    "Failed to save file." );
                        }
                    }
                }

                parent.dispose();
            } else {

                JOptionPane.showMessageDialog( parent,
                        "Could not connect with given info.",
                        "Error connecting!", JOptionPane.ERROR_MESSAGE );

            }

        }
    }
}
